package com.huazai.juc.cec.thread.past;

import lombok.extern.slf4j.Slf4j;

import java.util.Hashtable;
import java.util.concurrent.locks.LockSupport;

/**
 * 这个例子不知道为什么不对啊，到时候晚上再去看下juc的视频吧。
 */
@Slf4j(topic = "wad.demo")
public class WaitAwakeDemo {
    static final Object room = new Object();
    static boolean hasCigarette = false; // 有没有烟？不太理解。。。
    static boolean hasTakeout = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (room) {
                log.debug("有烟没？[{}]", hasCigarette);
                while (!hasCigarette) {
                    log.debug("还没有烟，先歇会");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没[{}]", hasCigarette);
                if (hasCigarette) {
                    log.debug("可以干活了");
                } else {
                    log.debug("没干成活");
                }
            }
        },"小南").start();


        new Thread(() -> {
            synchronized (room) {
                log.debug("外卖送到了没？[{}]", hasTakeout);
                if (!hasTakeout) {
                    log.debug("外卖还没送到，先歇会");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("外卖送到了吗[{}]", hasTakeout);
                if (hasTakeout) {
                    log.debug("可以干活了");
                } else {
                    log.debug("没干成活");
                }
            }
        }, "小女").start();

        Thread.sleep(1000);

        new Thread(() -> {
            hasTakeout = true;
            hasCigarette = true;
            log.debug("外卖到了哦");
        }, "外卖小哥").start();
    }


}
